home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / sbcp.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.5 KB  |  254 lines

  1. /* Copyright (C) 1993, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: sbcp.c,v 1.3 2000/09/19 19:00:47 lpd Exp $ */
  20. /* BCP and TBCP filters */
  21. #include "stdio_.h"
  22. #include "strimpl.h"
  23. #include "sbcp.h"
  24.  
  25. #define CtrlA 0x01
  26. #define CtrlC 0x03
  27. #define CtrlD 0x04
  28. #define CtrlE 0x05
  29. #define CtrlQ 0x11
  30. #define CtrlS 0x13
  31. #define CtrlT 0x14
  32. #define ESC 0x1b
  33. #define CtrlBksl 0x1c
  34.  
  35. /* The following is not used yet. */
  36. /*private const char *TBCP_end_protocol_string = "\033%-12345X"; */
  37.  
  38. /* ------ BCPEncode and TBCPEncode ------ */
  39.  
  40. /* Process a buffer */
  41. private int
  42. s_xBCPE_process(stream_state * st, stream_cursor_read * pr,
  43.         stream_cursor_write * pw, bool last, const byte * escaped)
  44. {
  45.     const byte *p = pr->ptr;
  46.     const byte *rlimit = pr->limit;
  47.     uint rcount = rlimit - p;
  48.     byte *q = pw->ptr;
  49.     uint wcount = pw->limit - q;
  50.     const byte *end = p + min(rcount, wcount);
  51.  
  52.     while (p < end) {
  53.     byte ch = *++p;
  54.  
  55.     if (ch <= 31 && escaped[ch]) {
  56.         if (p == rlimit) {
  57.         p--;
  58.         break;
  59.         }
  60.         *++q = CtrlA;
  61.         ch ^= 0x40;
  62.         if (--wcount < rcount)
  63.         end--;
  64.     }
  65.     *++q = ch;
  66.     }
  67.     pr->ptr = p;
  68.     pw->ptr = q;
  69.     return (p == rlimit ? 0 : 1);
  70. }
  71.  
  72. /* Actual process procedures */
  73. private int
  74. s_BCPE_process(stream_state * st, stream_cursor_read * pr,
  75.            stream_cursor_write * pw, bool last)
  76. {
  77.     static const byte escaped[32] =
  78.     {
  79.     0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  80.     0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0
  81.     };
  82.  
  83.     return s_xBCPE_process(st, pr, pw, last, escaped);
  84. }
  85. private int
  86. s_TBCPE_process(stream_state * st, stream_cursor_read * pr,
  87.         stream_cursor_write * pw, bool last)
  88. {
  89.     static const byte escaped[32] =
  90.     {
  91.     0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  92.     0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0
  93.     };
  94.  
  95.     return s_xBCPE_process(st, pr, pw, last, escaped);
  96. }
  97.  
  98. /* Stream templates */
  99. const stream_template s_BCPE_template =
  100. {&st_stream_state, NULL, s_BCPE_process, 1, 2
  101. };
  102. const stream_template s_TBCPE_template =
  103. {&st_stream_state, NULL, s_TBCPE_process, 1, 2
  104. };
  105.  
  106. /* ------ BCPDecode and TBCPDecode ------ */
  107.  
  108. private_st_BCPD_state();
  109.  
  110. /* Initialize the state */
  111. private int
  112. s_BCPD_init(stream_state * st)
  113. {
  114.     stream_BCPD_state *const ss = (stream_BCPD_state *) st;
  115.  
  116.     ss->escaped = 0;
  117.     ss->matched = ss->copy_count = 0;
  118.     return 0;
  119. }
  120.  
  121. /* Process a buffer */
  122. private int
  123. s_xBCPD_process(stream_state * st, stream_cursor_read * pr,
  124.         stream_cursor_write * pw, bool last, bool tagged)
  125. {
  126.     stream_BCPD_state *const ss = (stream_BCPD_state *) st;
  127.     const byte *p = pr->ptr;
  128.     const byte *rlimit = pr->limit;
  129.     byte *q = pw->ptr;
  130.     byte *wlimit = pw->limit;
  131.     int copy_count = ss->copy_count;
  132.     int status;
  133.     bool escaped = ss->escaped;
  134.  
  135.     for (;;) {
  136.     byte ch;
  137.  
  138.     if (copy_count) {
  139.         if (q == wlimit) {
  140.         status = (p < rlimit ? 1 : 0);
  141.         break;
  142.         }
  143.         *++q = *++(ss->copy_ptr);
  144.         copy_count--;
  145.         continue;
  146.     }
  147.     if (p == rlimit) {
  148.         status = 0;
  149.         break;
  150.     }
  151.     ch = *++p;
  152.     if (ch <= 31)
  153.         switch (ch) {
  154.         case CtrlA:
  155.             if (escaped) {
  156.             status = ERRC;
  157.             goto out;
  158.             }
  159.             escaped = true;
  160.             continue;
  161.         case CtrlC:
  162.             status = (*ss->signal_interrupt) (st);
  163.             if (status < 0)
  164.             goto out;
  165.             continue;
  166.         case CtrlD:
  167.             if (escaped) {
  168.             status = ERRC;
  169.             goto out;
  170.             }
  171.             status = EOFC;
  172.             goto out;
  173.         case CtrlE:
  174.             continue;
  175.         case CtrlQ:
  176.             continue;
  177.         case CtrlS:
  178.             continue;
  179.         case CtrlT:
  180.             status = (*ss->request_status) (st);
  181.             if (status < 0)
  182.             goto out;
  183.             continue;
  184.         case CtrlBksl:
  185.             continue;
  186.         }
  187.     if (q == wlimit) {
  188.         p--;
  189.         status = 1;
  190.         break;
  191.     }
  192.     if (escaped) {
  193.         escaped = false;
  194.         switch (ch) {
  195.         case '[':
  196.             if (!tagged) {
  197.             status = ERRC;
  198.             goto out;
  199.             }
  200.             /* falls through */
  201.         case 'A':
  202.         case 'C':
  203.         case 'D':
  204.         case 'E':
  205.         case 'Q':
  206.         case 'S':
  207.         case 'T':
  208.         case '\\':
  209.             ch ^= 0x40;
  210.             break;
  211.         case 'M':
  212.             if (!tagged) {
  213.             status = ERRC;
  214.             goto out;
  215.             }
  216.             continue;
  217.         default:
  218.             status = ERRC;
  219.             goto out;
  220.         }
  221.     }
  222.     *++q = ch;
  223.     }
  224.   out:ss->copy_count = copy_count;
  225.     ss->escaped = escaped;
  226.     pr->ptr = p;
  227.     pw->ptr = q;
  228.     return status;
  229. }
  230.  
  231. /* Actual process procedures */
  232. private int
  233. s_BCPD_process(stream_state * st, stream_cursor_read * pr,
  234.            stream_cursor_write * pw, bool last)
  235. {
  236.     return s_xBCPD_process(st, pr, pw, last, false);
  237. }
  238. private int
  239. s_TBCPD_process(stream_state * st, stream_cursor_read * pr,
  240.         stream_cursor_write * pw, bool last)
  241. {
  242.     return s_xBCPD_process(st, pr, pw, last, true);
  243. }
  244.  
  245. /* Stream templates */
  246. const stream_template s_BCPD_template =
  247. {&st_BCPD_state, s_BCPD_init, s_BCPD_process, 1, 1,
  248.  NULL, NULL, s_BCPD_init
  249. };
  250. const stream_template s_TBCPD_template =
  251. {&st_BCPD_state, s_BCPD_init, s_TBCPD_process, 1, 1,
  252.  NULL, NULL, s_BCPD_init
  253. };
  254.